import {
  Entity,
  Column,
  PrimaryGeneratedColumn,
  CreateDateColumn,
  UpdateDateColumn,
  OneToMany,
  AfterLoad
} from "typeorm"
import { Article } from "./Article";

import moment from "moment/moment"

moment.locale('zh-cn')

@Entity()
export class Category {
  @PrimaryGeneratedColumn()
  id: number // 分类id

  @Column({ comment: "分类名称" })
  name: string // 分类名称

  @OneToMany(() => Article, (article) => article.category)
  articles: Article[] // 拥有的文章

  @CreateDateColumn()
  createdAt: Date | string;

  @UpdateDateColumn()
  updatedAt: Date | string;

  @AfterLoad()
  formatDate() {
    this.createdAt = moment(this.createdAt).format('LL')
    this.updatedAt = moment(this.updatedAt).format('LL')
  }
}